package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Resource
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Resource
	private BaseAttributeMapper baseAttributeMapper;


	@Override
	public List<JSONObject> getBaseCategoryList() {
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);

		if (CollectionUtils.isEmpty(baseCategoryViewList)){
			return null;
		}

		List<JSONObject> jsonObjectList=new ArrayList<>();
		Map<Long, List<BaseCategoryView>> category1Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		category1Map.forEach((category1Id,category2List)->{
			JSONObject jsonObject1Map = new JSONObject();
			jsonObjectList.add(jsonObject1Map);
			jsonObject1Map.put("categoryName",category2List.get(0).getCategory1Name());
			jsonObject1Map.put("categoryId",category1Id);

			List<JSONObject> jsonObject2List = new ArrayList<>();
			jsonObject1Map.put("categoryChild",jsonObject2List);
			Map<Long, List<BaseCategoryView>> category2Map = category2List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			category2Map.forEach((category2Id,category3List)->{
				JSONObject jsonObject2Map = new JSONObject();
				jsonObject2List.add(jsonObject2Map);
				jsonObject2Map.put("categoryName",category3List.get(0).getCategory2Name());
				jsonObject2Map.put("categoryId",category2Id);

				jsonObject2Map.put("categoryChild",category3List.stream().map(category3->{
					JSONObject jsonObject3Map = new JSONObject();
					jsonObject3Map.put("categoryName",category3.getCategory3Name());
					jsonObject3Map.put("categoryId",category3.getCategory3Id());
					return jsonObject3Map;
				}).collect(Collectors.toList()));

			});
		});
		return jsonObjectList;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long categoryId) {
		return baseAttributeMapper.findAttribute(categoryId);
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		if (CollectionUtils.isEmpty(baseCategory2List)){
			return null;
		}
		List<Long> category2IdList = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().in(BaseCategory3::getCategory2Id, category2IdList)
				.eq(BaseCategory3::getIsTop, 1)
				.last("limit 7"));
		return baseCategory3List;
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id,category1Id));
		if (CollectionUtils.isEmpty(baseCategoryViewList)){
			return null;
		}

		JSONObject jsonObject1Map = new JSONObject();
		jsonObject1Map.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		jsonObject1Map.put("categoryId",category1Id);

			List<JSONObject> jsonObject2List = new ArrayList<>();
			jsonObject1Map.put("categoryChild",jsonObject2List);
			Map<Long, List<BaseCategoryView>> category2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			category2Map.forEach((category2Id,category3List)->{
				JSONObject jsonObject2Map = new JSONObject();
				jsonObject2List.add(jsonObject2Map);
				jsonObject2Map.put("categoryName",category3List.get(0).getCategory2Name());
				jsonObject2Map.put("categoryId",category2Id);

				jsonObject2Map.put("categoryChild",category3List.stream().map(category3->{
					JSONObject jsonObject3Map = new JSONObject();
					jsonObject3Map.put("categoryName",category3.getCategory3Name());
					jsonObject3Map.put("categoryId",category3.getCategory3Id());
					return jsonObject3Map;
				}).collect(Collectors.toList()));

			});
		return jsonObject1Map;
	}
}
